package com.ruoyi.common.tools.zip;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.lang3.math.NumberUtils;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.oss.core.OssClient;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyZipUtil {

	/**
	 * 处理zip
	 * 
	 * @param fileNameIds
	 * @param importType
	 * @param sysOssService
	 * @return
	 */
	public static List<OssZipContent> getOssZipContents(String[] fileNameIds, String importType) {
		List<Long> ids = Arrays.stream(fileNameIds).map(s -> NumberUtils.toLong(s)).collect(Collectors.toList());
		List<OssZipContent> result = new ArrayList<>(fileNameIds.length);

		List<SysOssVo> ossList = SpringUtils.getBean(ISysOssService.class).listByIds(ids);

		for (SysOssVo oss : ossList) {
			List<ZipContent> zipContentList = MyZipUtil.getZipContents(oss.getUrl(), importType);
			OssZipContent ossZip = new OssZipContent();
			ossZip.setOssId(oss.getOssId().toString());
			ossZip.setOssUrl(oss.getUrl());
			ossZip.setFileName(oss.getOriginalName());
			ossZip.setImportType(importType);
			ossZip.setZipContentList(zipContentList);

			result.add(ossZip);
		}
		return result;
	}

	/**
	 * 下载zip 解析 zip内容
	 * 
	 * @param downloadZipUrl
	 * @param importType
	 * @return zip内容List
	 */
	public static List<ZipContent> getZipContents(String downloadZipUrl, String importType) {
		OssClient storage = OssFactory.instance();
		InputStream inputStream = storage.getObjectContent(downloadZipUrl);

		List<ZipContent> zipContentList = MyZipUtil.readZipContent(inputStream, importType, true);

		return zipContentList;
	}

	public static List<ZipContent> readZipContent(InputStream is, String importType, boolean readZipInputStream) {
		return readZipContent(is, importType, readZipInputStream, null);
	}

	public static List<ZipContent> readZipContent(InputStream is, String importType, boolean readZipInputStream,
			String charset) {
		List<ZipContent> list = new ArrayList<>(100);
		if (StringUtils.isEmpty(charset)) {
			charset = "GBK";
		}
		try (ZipInputStream zipInputStream = new ZipInputStream(is, Charset.forName(charset))) {
			ZipEntry zipEntry;
			while ((zipEntry = zipInputStream.getNextEntry()) != null) {
				String zipEntryNameStr = zipEntry.getName();
				if (zipEntry.isDirectory() || zipEntryNameStr.indexOf("_MACOSX") != -1
						|| zipEntryNameStr.indexOf(".DS_Store") != -1) {

				} else {
					ZipContent content = new ZipContent();
					content.setFileName(zipEntryNameStr);
					content.setImportType(importType);
					if (readZipInputStream) {
						content.setFileIs(IoUtil.readBytes(zipInputStream, false));
					}

					int ii1 = zipEntryNameStr.lastIndexOf("/");
					int ii2 = zipEntryNameStr.lastIndexOf(".");
					String suffix = zipEntryNameStr.substring(ii2);
					String name = zipEntryNameStr.substring(ii1 + 1).replace(suffix, "");

					content.setSuffix(suffix);
					content.setName(name);
					list.add(content);
				}
				zipInputStream.closeEntry();
			}

			zipInputStream.close();
			log.info("处理zip完成");
		} catch (IOException e) {
			log.error("压缩包处理异常，异常信息{}" + e, e);
		}

		return list;
	}
}
